package com.rhb.ca.web.online;

import java.util.ArrayList;
import java.util.Hashtable;

public class Listener extends Thread{
    private static Hashtable uzers;
    private static ArrayList onlines;
    private boolean listening;

    public Listener(ArrayList onlines, Hashtable uzers) throws Exception{
        super("OnlineListener");
        this.listening = true;
        this.onlines = onlines;
        this.uzers = uzers;
    }

    public void run(){
        try{
            listener();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    private void listener() throws Exception{
        while(listening){
            synchronized(onlines){
                int i = 0;
                for(int si = onlines.size(); i < si; i++){
                    OnLine l = (OnLine)onlines.get(i);
                    if(l == null){
                        onlines.remove(l);
                        i--;si--;
                    }else if(l.isOvertime()){
                        onlines.remove(l);
                        synchronized(uzers){
                            uzers.remove(l.getSessionID());
	                        i--;si--;
                        }
                    }
                }
            }
            sleep(1000);
        }
    }
}
